package com.superpai.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.superpai.R;
import com.superpai.api.ShopAPI;
import com.superpai.cache.CacheShop;
import com.superpai.common.Constants;
import com.superpai.response.shop.GetShopInfoResponse;

/**
 * @author Devin.Hu
 * @date 2011-8-26
 * @version V1.0
 * @description 关于页面
 */
public class AboutActivity extends BaseActivity {

	private GetShopInfoResponse result; 
	private final int GETSHOPINFO = 1;
	
	private TextView about_shopname;
	private TextView about_goodEval;
	private TextView about_normalEval;
	private TextView about_badEval;
	private TextView about_sellerLevelCount;
	private TextView about_goodDescriptionMatch;
	private TextView about_attitudeOfService;
	private TextView about_speedOfDelivery;
	
	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.setContentView(R.layout.about);
		initMenuView(this, mIndexMore);
		initViews();
	}
	
	private void initViews(){
		title = (TextView)this.findViewById(R.id.title);
		title.setText(R.string.more_about_title);
		
		mProgress = (ProgressBar)this.findViewById(R.id.progress);
		mRefresh = (Button)this.findViewById(R.id.refresh);
		mNodata = (TextView)this.findViewById(R.id.nodata);
		
		about_shopname = (TextView)this.findViewById(R.id.about_shopname);
		about_goodEval = (TextView)this.findViewById(R.id.about_goodEval);
		about_normalEval = (TextView)this.findViewById(R.id.about_normalEval);
		about_badEval = (TextView)this.findViewById(R.id.about_badEval);
		about_sellerLevelCount = (TextView)this.findViewById(R.id.about_sellerLevelCount);
		about_goodDescriptionMatch = (TextView)this.findViewById(R.id.about_goodDescriptionMatch);
		about_attitudeOfService = (TextView)this.findViewById(R.id.about_attitudeOfService);
		about_speedOfDelivery = (TextView)this.findViewById(R.id.about_speedOfDelivery);
		
		connectionDate();
	}


	/**
	 * 读取店铺信息
	 */
	private void connectionDate(){
		mRefresh.setVisibility(View.GONE);
		mNodata.setVisibility(View.GONE);
		mProgress.setVisibility(View.VISIBLE);
		onConnection(GETSHOPINFO);
	}
	
	@Override
	public Object doDataConnection(int type) {
		try {
			CacheShop cacheShop = new CacheShop(mContext);
			if(cacheShop.checkCacheShopInfo()){
				result = cacheShop.getCacheShopInfo();
				if(null != result){
					return STATUS_SUC;
				}
			}else{
				ShopAPI api = new ShopAPI();
				result = api.getShopInfo(Constants.sellerUin);
				if(result.isSuccess()){
					cacheShop.addCacheShopInfo(result);
					return STATUS_SUC;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return STATUS_ERR;
		}
		return STATUS_NODATA;
	}

	@Override
	public void doProcessData(int type, Object... values) {
		int status = Integer.parseInt(values[0].toString());
		mProgress.setVisibility(View.GONE);
		if(status == STATUS_SUC){
			about_shopname.setText(result.getShopName());
			about_goodEval.setText(result.getGoodEval()+"");
			about_normalEval.setText(result.getNormalEval()+"");
			about_badEval.setText(result.getBadEval()+"");
			about_sellerLevelCount.setText(result.getSellerLevelCount()+"");
			about_goodDescriptionMatch.setText(result.getGoodDescriptionMatch()+"");
			about_attitudeOfService.setText(result.getAttitudeOfService()+"");
			about_speedOfDelivery.setText(result.getSpeedOfDelivery()+"");
		}else if(status == STATUS_NODATA){
			mNodata.setVisibility(View.VISIBLE);
		}else{
			mRefresh.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		checkMenu(v);
		
		if(v == mRefresh){
			connectionDate();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		disConnection();
	}
}
